package com.android.xunxian.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.xunxian.entry.GuanW;
import com.android.xunxian.entry.GuanwType;
import com.android.xunxian.utils.UConstants;
import com.android.xunxian.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.oa.android.MyApplication;
import com.oa.android.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 系统设置
 * Created by zouqh on 2016/8/7.
 */
public class SettingActivity extends Activity implements View.OnClickListener{

    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.guanwText)
    TextView guanwText;
    @InjectView(R.id.layout1)
    RelativeLayout layout1;
    @InjectView(R.id.layout2)
    RelativeLayout layout2;
    @InjectView(R.id.layout3)
    RelativeLayout layout3;
    @InjectView(R.id.layout4)
    RelativeLayout layout4;
    private String string="管网信息更新";
    ProgressDialog dialog;
    public static SettingActivity activity;
    private List<GuanwType> guanwTypeList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity=this;
        setContentView(R.layout.settting_activity);
        ButterKnife.inject(this);
        titleText.setText("系统设置");
        close.setOnClickListener(this);
        layout1.setOnClickListener(this);
        layout2.setOnClickListener(this);
        layout3.setOnClickListener(this);
        layout4.setOnClickListener(this);
        try {
            guanwTypeList= MyApplication.dbManager.selector(GuanwType.class).findAll();
            if (guanwTypeList==null||guanwTypeList.size()<=0){
                string="管网信息下载";
                guanwText.setText(string);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){

            case R.id.close:
                finish();
                break;
            case R.id.layout1:
                intent.setClass(this, SaveServiceActivity.class);
                intent.putExtra("type", "2");
                startActivity(intent);
                break;
            case R.id.layout2:
                getGdType();
                break;
            case R.id.layout3:
                intent.setClass(this, GuanWActivity.class);
                startActivity(intent);
                break;
            case R.id.layout4:
                intent.setClass(this, UpdateSettingActivity.class);
                startActivity(intent);
                break;

        }
    }


    //获取管道类型
    private void getGdType() {
        dialog = ProgressDialog.show(this, null, string+"中...", true, false);
        RequestParams params = new RequestParams(UConstants.GD_TYPE);
        params.addBodyParameter("LKind", "CBJ");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("SelId", "GDLX");

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {


                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<GuanwType>>() {
                            }.getType();
                            List<GuanwType> list = gson.fromJson(result.toString(), lt);
                            if (list != null && list.size() > 0) {
                                if(guanwTypeList!=null){
                                    guanwTypeList.clear();
                                }else{
                                    guanwTypeList=new ArrayList<GuanwType>();
                            }

                                guanwTypeList.addAll(list);
                                try {
                                    MyApplication.dbManager.delete(GuanwType.class);
                                    MyApplication.dbManager.save(guanwTypeList);
                                    getGdDate();
                                } catch (DbException e) {
                                    e.printStackTrace();
                                    dialog.dismiss();
                                    Toast.makeText(x.app(), "保存异常", Toast.LENGTH_LONG).show();
                                }

                            } else{
                                dialog.dismiss();
                                Toast.makeText(x.app(), "暂无管网信息", Toast.LENGTH_LONG).show();
                            }


//                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();

                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }


    //获取管道数据
    private void getGdDate() {
        RequestParams params = new RequestParams(UConstants.GD_DATE);
        params.addBodyParameter("LKind", "CBJ");

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);

                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<GuanW>>() {
                            }.getType();
                            List<GuanW> list = gson.fromJson(object.getString("rows").toString(), lt);
//
                            if (list != null && list.size() > 0) {
//
                                try {
                                    MyApplication.dbManager.delete(GuanW.class);
                                    MyApplication.dbManager.save(list);
                                    Toast.makeText(x.app(), "完成下载", Toast.LENGTH_LONG).show();
                                } catch (DbException e) {
                                    e.printStackTrace();

                                    Toast.makeText(x.app(), "保存异常", Toast.LENGTH_LONG).show();
                                }

                            } else {
                                Toast.makeText(x.app(), "暂无数据！", Toast.LENGTH_LONG).show();
                            }

                        } else {
                            Toast.makeText(x.app(), "暂无数据！", Toast.LENGTH_LONG).show();
                        }
//                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
